﻿using DmxTestTool.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DmxTestTool
{
    /// <summary>
    /// AddDMXTestWindows.xaml 的交互逻辑
    /// </summary>
    public partial class AddDMXTestWindows : Window
    {
        public DMXTestInfo DMXTestInfo { set; get; }
        public AddDMXTestWindows()
        {
            InitializeComponent();
            DMXTestInfo = new DMXTestInfo();
            this.DataContext = DMXTestInfo;
            coms.ItemsSource= System.IO.Ports.SerialPort.GetPortNames();
            coms.SelectionChanged += Coms_SelectionChanged;
        }

        private void Coms_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DMXTestInfo.ComName = e.AddedItems[0].ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!(DMXTestInfo.StartIndex >=1 && DMXTestInfo.EndIndex <= 512 && DMXTestInfo.StartIndex >= 1 && DMXTestInfo.EndIndex <= 512))
                {
                    throw new Exception("通道值不合法");
                }
                if (DMXTestInfo.StartIndex > DMXTestInfo.EndIndex)
                {
                    throw new Exception("开始通道比结束通道大");

                }
                if (DMXTestInfo.sendType == 0)
                {
                    DMXTestInfo.TitleName = "artnet-" + DMXTestInfo.IP + "-" + DMXTestInfo.Universe;

                }
                else if (DMXTestInfo.sendType == 1)
                {
                    DMXTestInfo.TitleName = "RS485-" + DMXTestInfo.ComName;

                }
                else if (DMXTestInfo.sendType == 2)
                {
                    DMXTestInfo.TitleName = "OSC-" + DMXTestInfo.OSCIPEndPoint;

                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("创建失败：" + ex.Message);

                return;
            }
        
            DialogResult = true;

        }
    }
}
